//
//  MainUITabBarController.swift
//  Microblog
//
//  Created by steaest on 15/7/28.
//  Copyright © 2015年 steaest. All rights reserved.
//

//疑问1:只添加了普通图片,设置为橘黄色之后,为什么能够实现点击是橘黄色,不点击是灰色
//疑问2:重写的addChildViewController(vc:UIViewController,title: String,imageName:String)括号里面的是不是就是参数:是

import UIKit

class MainUITabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()

    }
    
    //此处加载按钮控件
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        setUpComposeButton()
    }
    
    
    //创建按钮
    private func setUpComposeButton() {
    
        let w = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        composeButton.frame = CGRectOffset(rect, w * 2, 0)
    }
    
    
    
    
    //快速加载子控制器
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "信息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProFileTableViewController(), title: "我", imageName: "tabbar_profile")
    
    }

    //重载系统方法
    private func addChildViewController(vc:UIViewController,title: String,imageName:String) {
       
        //tabBar.tintColor = UIColor.orangeColor()
        
        vc.title = title
        //title属性是由里向外添加的
        vc.tabBarItem.image = UIImage(named: imageName)
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
  
    }
    
    //按钮懒加载
    lazy private var composeButton:UIButton = {
    
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        self.tabBar.addSubview(btn)
        
        btn .addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    //按钮监听
    func clickComposeButton() {
    
        let vc = UserAccount.userLogon ? ComposeViewController() : OAuthViewController()
        
        let nav = UINavigationController(rootViewController: vc)
        
        presentViewController(nav, animated: true, completion: nil)
    }
    
    //设置按钮位置

}
